package angle;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

import common.BaseAction;
import common.CommonDict;
import common.Ip;
import entities.TSubject;
import interfaces.IMerchantService;
import interfaces.ISubjectService;
import util.LogUtil;
import utils.AesUtil;
import utils.StringUtil;

public class SubjectAngle extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private ISubjectService subjectService;
	private IMerchantService merchantService;

	private TSubject subjectBean;
	private List<TSubject> subjectList;

	private String subNo;
	private String subNos;
	private String subName;
	private String subPwd;
	private String subKey;
	private String subMenu;
	private String subState;
	private Date subDate;
	private Date subDateTop;
	private Date subDateEnd;
	private String subRemark;

	private String merNo;

	public String login() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		String ip = Ip.getIp(ServletActionContext.getRequest());
		HashMap<String, Object> paramMap = new HashMap<String, Object>();

		if (!ActionContext.getContext().getSession().containsKey("code")) {
			addActionError("验证码失效,请刷新");
			return ERROR;
		}
		if (StringUtil.isEmpty(code) || !code.equals(ActionContext.getContext().getSession().get("code").toString())) {
			addActionError("验证码错误");
			return ERROR;
		}
		if (StringUtil.isEmpty(subNo)) {
			addActionError("用户名不能为空");
			return ERROR;
		}
		if (StringUtil.isEmpty(subPwd)) {
			addActionError("密码不能为空");
			return ERROR;
		}
		
		try{
			paramMap.put("subNo", StringUtil.split(subNo));
			paramMap.put("subPwd", AesUtil.AES_Encrypt(AesUtil.KEY, subPwd));
			subjectBean = subjectService.single(paramMap);
		}catch(Exception e){
			addActionError("无法连接RPC数据服务");
			return ERROR;
		}

		if (StringUtil.isEmpty(subjectBean)) {
			addActionError("子账户用户名或密码错");
			return ERROR;
		}

		if (subjectBean.getSubState().equals("0")) {
			addActionError("账户已经被停用");
			return ERROR;
		}
		LogUtil.login("[" + ip + "]子账户号[" + subjectBean.getSubNo() + "]");		
		
		if(subjectBean.getSubNo().endsWith(CommonDict.SUBJECT_NO)){
			session.put("query", subjectBean.getSubNo().substring(0, 4));
		}else{
			session.put("query", subjectBean.getSubNo());
		}
		session.put("merchant", merchantService.fetch(subNo.substring(0, 4)));
		session.put("subject", subjectBean);
		return SUCCESS;

	}

	public String logout() throws Exception {
		ActionContext.getContext().getSession().clear();
		return SUCCESS;
	}
	
	public String logproxy() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("subNo", StringUtil.split(subNo));
		paramMap.put("subPwd", subPwd);
		subjectBean = subjectService.single(paramMap);
		if (StringUtil.isEmpty(subjectBean)) {
			addActionError("子账户用户名或密码错");
			return ERROR;
		}
		if(subjectBean.getSubNo().endsWith(CommonDict.SUBJECT_NO)){
			session.put("query", subjectBean.getSubNo().substring(0, 4));
		}else{
			session.put("query", subjectBean.getSubNo());
		}
		session.put("merchant", merchantService.fetch(subNo.substring(0, 4)));
		session.put("subject", subjectBean);
		return SUCCESS;
	}

	public String logadmin() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("subNo", StringUtil.split(subNo));
		subjectBean = subjectService.single(paramMap);
		if(subjectBean.getSubNo().endsWith(CommonDict.SUBJECT_NO)){
			session.put("query", subjectBean.getSubNo().substring(0, 4));
		}else{
			session.put("query", subjectBean.getSubNo());
		}
		session.put("merchant", merchantService.fetch(subNo.substring(0, 4)));
		session.put("subject", subjectBean);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public ISubjectService getSubjectService() {
		return subjectService;
	}

	public void setSubjectService(ISubjectService subjectService) {
		this.subjectService = subjectService;
	}

	public TSubject getSubjectBean() {
		return subjectBean;
	}

	public void setSubjectBean(TSubject subjectBean) {
		this.subjectBean = subjectBean;
	}

	public List<TSubject> getSubjectList() {
		return subjectList;
	}

	public void setSubjectList(List<TSubject> subjectList) {
		this.subjectList = subjectList;
	}

	public String getSubNo() {
		return subNo;
	}

	public void setSubNo(String subNo) {
		this.subNo = subNo;
	}

	public String getSubNos() {
		return subNos;
	}

	public void setSubNos(String subNos) {
		this.subNos = subNos;
	}

	public String getSubName() {
		return subName;
	}

	public void setSubName(String subName) {
		this.subName = subName;
	}

	public String getSubPwd() {
		return subPwd;
	}

	public void setSubPwd(String subPwd) {
		this.subPwd = subPwd;
	}

	public String getSubKey() {
		return subKey;
	}

	public void setSubKey(String subKey) {
		this.subKey = subKey;
	}

	public String getSubMenu() {
		return subMenu;
	}

	public void setSubMenu(String subMenu) {
		this.subMenu = subMenu;
	}

	public String getSubState() {
		return subState;
	}

	public void setSubState(String subState) {
		this.subState = subState;
	}

	public Date getSubDate() {
		return subDate;
	}

	public void setSubDate(Date subDate) {
		this.subDate = subDate;
	}

	public Date getSubDateTop() {
		return subDateTop;
	}

	public void setSubDateTop(Date subDateTop) {
		this.subDateTop = subDateTop;
	}

	public Date getSubDateEnd() {
		return subDateEnd;
	}

	public void setSubDateEnd(Date subDateEnd) {
		this.subDateEnd = subDateEnd;
	}

	public String getSubRemark() {
		return subRemark;
	}

	public void setSubRemark(String subRemark) {
		this.subRemark = subRemark;
	}

	public String getMerNo() {
		return merNo;
	}

	public void setMerNo(String merNo) {
		this.merNo = merNo;
	}

	public IMerchantService getMerchantService() {
		return merchantService;
	}

	public void setMerchantService(IMerchantService merchantService) {
		this.merchantService = merchantService;
	}

	
	
}
